# Title     : 北京市二手房交易分析
# Objective : TODO
# Created by: Administrator
# Created on: 2021/6/4
data <- read.csv("F:/bj.csv", encoding = "utf-8")
head(data)
# 数据预处理
names(data)[names(data) == "小区"] <- "xiaoqu"
names(data)[names(data) == "户型"] <- "huxing"
names(data)[names(data) == "面积"] <- "Area"
names(data)[names(data) == "观看次数"] <- "Watch_number"
names(data)[names(data) == "城区"] <- "Dist"
names(data)[names(data) == "楼层"] <- "Floor"
names(data)[names(data) == "房价"] <- "Total_price"
names(data)[names(data) == "时间"] <- "Release_Time"
names(data)[names(data) == "关注人数"] <- "Heat"
names(data)[9] <- "Price"
names(data)[11] <- "Lotitude"
head(data)
# 此句超慢，演示慎用
pairs(~Area +
  Heat +
  Watch_number +
  Release_Time +
  Total_price +
  Price, data = data, main = "Main Scatterplot")
summary(data)
# 画直方图 各区单价直方图
library(lattice)
histogram(~Price | Dist, data = data, main = "Density of Price in Every Dist", xlab = "Yuan/m2")
# 条形图 各区房价均值条形图
mpd <- aggregate(data$Price, by = list(type = data$Dist), mean)
head(mpd[order(mpd$x),]) # mean Price Dict
barplot(mpd$x, names.arg = mpd$type, main = "Mean of Every Dists")
#
summary(data$Dist)
# 再画一个直方图 全北京每平米密度分布图
library(ggplot2)
ggplot(data = data, aes(x = Price)) +
  geom_histogram(aes(y = ..density..), color = "black", fill = "white") +
  geom_density(alpha = .2, fill = "red")
# 散点图 面积与单价、总价之关系
par(mfrow = c(1, 1))
plot(x = data$Area, y = data$Price, col = "red")
plot(x = data$Area, y = data$Total_price, col = "blue")
mpa <- aggregate(data$Price, by = list(type = data$Area), mean)
head(mpa[order(mpa$x),])
mtpa <- aggregate(data$Total_price, by = list(type = data$Area), mean)
head(mtpa[order(mtpa$x),])
# 户型价格的关系
table(data$huxing)
mphx <- aggregate(data$Price, by = list(type = data$huxing), mean)
head(mphx[order(mphx$x),])
plot(x = data$huxing, y = data$Price, col = "green")
# 房价与热度看房人数的关系
install.packages('devtools')
devtools::install_github("AckerDWM/gg3D")
library(plot3D)
scatter3D(x = data$Heat, y = data$Watch_number, z = data$Price, pch = 21, cex = 1.5, col = "black", bg = "#F57446",
          xlab = "Heat", ylab = "Watch Numer", zlab = "Price",
          ticktype = "detailed", bty = "f", box = TRUE,
          theta = 0, phi = 20, d = 3,
          colkey = FALSE)
PH <- ggplot(data = data, aes(data$Price, data$Heat)) +
  geom_point(colour = "Red", alpha = 0.1) +
  labs(x = "Price", y = "Heat")
PWN <- ggplot(data = data, aes(data$Price, data$Watch_number)) +
  geom_point(colour = "Blue", alpha = 0.1) +
  labs(x = "Price", y = "Watch Number")
library(gridExtra)
grid.arrange(PH, PWN, ncol = 2)
#分析热度与发布时间是否有相关性
cor(data$Heat, data$Release_Time,method = "pearson")
cor(data$Heat, data$Release_Time,method = "spearman")
cor(data$Heat, data$Release_Time,method = "kendall")
plot(x = data$Release_Time, y = data$Heat, col = "green")
#聚类分析
library(tidyverse)
data2 <- separate(data = data, col = Lotitude, into = c("East", "North"), sep = ",")
head(data2)
East <- as.numeric(data2$East)
North <- as.numeric(data2$North)
Price <- as.numeric(data2$Price)
data3 <- data.frame(East, North, Price)
head(data3)
install.packages("rgl")
library(rgl)
plot3d(data3$East, data3$North, data3$Price, col = "red")
install.packages("factoextra")
library(factoextra)
data3 <- scale(data3)
sub <- sample(nrow(data3), 12000, replace = F)
data4 <- data3[sub,]
fviz_nbclust(data4, kmeans, method = "wss") + geom_vline(xintercept = 4, linetype = 2)
East <- as.numeric(data2$East)
North <- as.numeric(data2$North)
Price <- as.numeric(data2$Price)
data3 <- data.frame(East, North, Price)
kmeans(data3, 4)
#knn
head(data2)
# a<- model.matrix(~Dist-1,data) %>% as.data.frame()
# nrow(a)
data5 <- data2[, c("Area", "Total_price", "Heat", "Watch_number", "Release_Time", "Price")]
data5 <- data.frame(as.numeric(data2$East), as.numeric(data2$North), data5)
names(data5)[names(data5) == "as.numeric.data2.East."] <- "East"
names(data5)[names(data5) == "as.numeric.data2.North."] <- "North"
head(data5)
data6 <- data5 %>% mutate(Price = (Price >= 59582.43 & !is.na(Price)) * NA^(is.na(Price)))
# 关联性分析
install.packages("corrgram")
library(corrgram)
corrgram(data5, order = TRUE, lower.panel = panel.shade, upper.panel = panel.pts, text.panel = panel.txt)
# knn
library(class)
head(data6)
data6 <- data6[, c("Total_price", "Watch_number", "North", "East", "Price")]
train_sub = sample(nrow(data6), nrow(data6) * .7)
train_data = data6[train_sub,]
test_data = data6[-train_sub,]

krate <- data.frame(
  k = 1,
  rate = 1)

krate
for (i in 2:30) {
  knn_pred <- knn(
    train = subset(train_data, select = -Price),
    test = subset(test_data, select = -Price),
    cl = train_data$Price, k = i
  )
  a <- table(test_data$Price, knn_pred)
  wrong <- sum(data.frame(a)$Freq[2:3])
  tmp <- data.frame(
    k = i,
    rate = wrong / nrow(test_data)
  )
  tmp
  krate <- rbind(krate, tmp)
}
plot(krate, type = "l")
axis(1, 1:30, 1:30)
abline(v = 3, lyt = 2, col = "blue")


knn_pred <- knn(
  train = subset(train_data, select = -Price),
  test = subset(test_data, select = -Price),
  cl = train_data$Price, k = 3
)

table(test_data$Price, knn_pred)

